package com.xyzwps.mvckotlin.handler

import com.xyzwps.mvckotlin.config.Const
import com.xyzwps.mvckotlin.randomString
import org.springframework.stereotype.Component
import org.springframework.web.server.ServerWebExchange
import org.springframework.web.server.WebFilter
import org.springframework.web.server.WebFilterChain
import reactor.core.publisher.Mono

@Suppress("unused")
@Component
class XRequestIdFilter : WebFilter {
    override fun filter(exchange: ServerWebExchange, chain: WebFilterChain): Mono<Void> {
        val request = exchange.request
        val xRequestId: String = request.headers[Const.X_REQ_ID]?.first() ?: randomString()
        request.mutate().header(Const.X_REQ_ID, xRequestId).build()
        return chain.filter(exchange)
    }
}